using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Iservice;
using Demo.Net.Model;
using service;
using Utils;

namespace Demo.Net.BlogAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class FeedBackController : ControllerBase
    {
        private readonly IBaseService<Feedback> _feedback;

        public FeedBackController(IBaseService<Feedback> feedback)
        {
            this._feedback = feedback;
        }
         //查找全部
        [HttpGet]
        public async Task<List<Feedback>> FindAll()
        {
            return await _feedback.FindAll();
        }
        // 根据搜索条件查询反馈信息
        [HttpGet]
        public async Task<List<Feedback>> FindAllByCondition(string searchTerm)
        {
            return await _feedback.FindAllByCondition(
                s => s.Id.ToString() == searchTerm
                || s.Owner.Name.Contains(searchTerm)
                || s.Title.Contains(searchTerm)
                );
        }
        //根据id查找
        [HttpGet]
        public async Task<List<Feedback>> Find(int id)
        {
            return await _feedback.Find(s => s.Id == id);
        }
        //添加
        [HttpPost]
        public async Task<bool> Add(Feedback feedBacks)
        {
            return await _feedback.Add(feedBacks);
        }
        //编辑
        [HttpPut]
        public async Task<ActionResult<ApiReturn>> Update(Feedback feedBacks)
        {
            var res = await _feedback.UpdateAsync(feedBacks);
            if (res == false)
            {
                return ApiRetrunsHelper.Failed("没有数据");
            }
            return ApiRetrunsHelper.Success(res);
        }
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiReturn>> Delect(int id)
        {
            var result = await _feedback.Delete(id);
            if (result)
            {
                return ApiRetrunsHelper.Success(result);
            }
            else
            {
                return ApiRetrunsHelper.Failed("没有数据");
            }

        }
    }
}